package com.huang.resp;

import com.huang.enums.CommonCodeEnum;
import com.huang.enums.ResultStatusCode;

import java.io.Serializable;

/**
 * 通用结果CommonResult
 * msg : 结果描述
 * flag : 结果标识    true-成功、false-失败
 *
 */
public class CommonResult implements Serializable {

    private static final long serialVersionUID = 7465342926652146871L;

    private Integer code;
    private String msg;
    private Object data;
    public CommonResult() {}
    public CommonResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResult(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static CommonResult error() {
        return new CommonResult(CommonCodeEnum.SERVER_ERROR.getCode(), CommonCodeEnum.SERVER_ERROR.getMsg());
    }

    public static CommonResult error(ResultStatusCode resultStatusCode) {
        return new CommonResult(resultStatusCode.getCode(), resultStatusCode.getMsg());
    }

    public static CommonResult error(String msg) {
        return new CommonResult(CommonCodeEnum.PARAM_ERROR.getCode(), msg);
    }

    public static CommonResult error(ResultStatusCode resultStatusCode, String errorMsg) {
        String msg = String.format(resultStatusCode.getMsg(), errorMsg);
        return new CommonResult(resultStatusCode.getCode(), msg);
    }



    public static CommonResult error(ResultStatusCode resultStatusCode, Object data) {
        return new CommonResult(resultStatusCode.getCode(), resultStatusCode.getMsg(), data);
    }

    public static CommonResult success() {
        return new CommonResult(CommonCodeEnum.SUCCESS.getCode(), CommonCodeEnum.SUCCESS.getMsg());
    }

    public static CommonResult success(ResultStatusCode resultStatusCode) {
        return new CommonResult(resultStatusCode.getCode(), resultStatusCode.getMsg());
    }

    public static CommonResult success(Object data) {
        return new CommonResult(CommonCodeEnum.SUCCESS.getCode(), CommonCodeEnum.SUCCESS.getMsg(), data);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "CommonResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
